﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;

namespace NPMonitor.Launcher
{
    public class SingleInstanceManager : WindowsFormsApplicationBase
    {
        MainForm mainForm;

        public SingleInstanceManager(MainForm mainForm)
        {
            this.IsSingleInstance = true;
            this.mainForm = mainForm;
        }

        protected override bool OnStartup(StartupEventArgs eventArgs)
        {
            if (mainForm != null)
            {
                Application.Run(mainForm);
            }

            return false;
        }

        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
        {
            base.OnStartupNextInstance(eventArgs);

            if (mainForm != null)
            {
                mainForm.Activate();

                MessageBox.Show("There is one running application instance, can't start another instance.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }
    }
}
